package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.forlove.app.qselect.login.service.ILoginInputPwService;
import com.forlove.app.qselect.login.service.impl.LoginLoginPWServiceImpl;
import com.forlove.app.qselect.login.view.ILoginInputPWView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/18.
 */

public class LoginInputPwPresenter extends IBasePresenter {
    private ILoginInputPWView loginInputPWView;
    private ILoginInputPwService loginInputPwService;

    public LoginInputPwPresenter(ILoginInputPWView loginInputPWView) {
        this.loginInputPWView = loginInputPWView;
        loginInputPwService = new LoginLoginPWServiceImpl();
        setService(loginInputPwService);
    }


    public void loadLoginPwData(final LoginEntity loginEntity) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", loginEntity.mobile);
        param.put("device_no", loginEntity.device_no);
        param.put("mobile_type", loginEntity.mobile_type);
        param.put("location", loginEntity.location);
        param.put("password", loginEntity.password);
        loginInputPwService.loadData(ServerManager.ACTION_LOGIN_PW, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "登录成功：" + result);
                UserEntity userEntity = ParseHelper.getModel(result, "data", UserEntity.class);
                loginInputPWView.checkPWView(NetworkManager.ResultCode.RESULT_SUCCESS, userEntity, "登录成功！");
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "登录失败：" + result);
                String msg = ParseHelper.getString(result, "message");
                if (TextUtils.isEmpty(msg)) {
                    msg = "稍后重试！";
                }
                loginInputPWView.checkPWView(NetworkManager.ResultCode.RESULT_ERROR, null, msg);
            }
        });
    }

    public void loadSendMsg(final LoginEntity loginEntity,String sign) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", loginEntity.mobile);
        param.put("sign", sign);
        loginInputPwService.loadData(ServerManager.ACTION_RESETPW_MSG, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                loginEntity.parseJSONString(ParseHelper.getString(result, "data"));
                loginInputPWView.showSendMsgView(loginEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    loginInputPWView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    loginInputPWView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");

                }
            }
        });
    }

    public void loadUpJPushRegisterId(Map param) {
        loginInputPwService.loadData(ServerManager.ACTION_UP_REGISTERID, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "上传jpush id:" + result);
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "上传jpush id:" + result);
            }
        });
    }
}
